/**
 * Created Date: Friday, April 7th 2023, 12:28:01 am
 * Author: CodingGorit
 * -----
 * Last Modified: Sun Apr 09 2023
 * Modified By: CodingGorit
 * -----
 * Copyright © 2019 —— 2023 fmin-courses All Rights Reserved
 * ------------------------------------
 * Javascript will save your soul!
 */
import type { App } from 'vue';
import { createRouter, createWebHistory, RouterOptions, Router } from 'vue-router';
import routes from './router.js';
import NProgress from 'nprogress';
import 'nprogress/nprogress.css';
import pina from '@/store';
import { storeToRefs } from 'pinia';
import { useAdminStore } from '@/store/AdminStore';

const adminStore = useAdminStore(pina);

const { loginState } = storeToRefs(adminStore);

const options: RouterOptions = {
    history: createWebHistory(),
    routes
}

const router: Router = createRouter(options);

// 路由首尾配置
router.beforeEach((to, from, next) => {
    NProgress.start()
    
    // 登陆路由
    // if (!loginState.value && to.name !== 'Login') {
    //     next({ name: "Login"});
    // } else 
    
    if (to.matched.length !== 0) {
        next();
    } else {
        next({ path: '/404' });
    }
});

router.afterEach(() => {
    NProgress.done();
});

/**
 * register router
 * @param app 
 */
const setupRouter = (app: App<Element>) => {
    app.use(router);
};

export default setupRouter;

